﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;

namespace wssServer
{
    class Functions
    {

        static public string lerRegKey(string keyName)
        {
            RegistryKey RegManager = Registry.LocalMachine;

            RegistryKey SubKey = RegManager.OpenSubKey("Software\\WssServer");

            return (string)SubKey.GetValue(keyName);
        }

        static public string lerSubKey(string SubKey, string keyName)
        {
            RegistryKey RegManager = Registry.LocalMachine;

            RegistryKey RegKey = RegManager.OpenSubKey("Software\\WssServer\\" + SubKey);

            return (string)RegKey.GetValue(keyName);
        }

        static public string[] splitMessage(string Message)
        {
            string[] splitParam = new string[1];
            splitParam.SetValue("||", 0);

            StringSplitOptions splitOption = new StringSplitOptions();

            return Message.Split(splitParam, splitOption);
        }

        static public bool ValidarLicenca(string Licenca)
        {
            int soma = 0;

            if (Licenca.Length != 16) return false;

            for (int i = 0; i < 12; i++)
            {
                char letra = Licenca[i];
                soma += Convert.ToInt32(letra) * (12 - i);
            }
            for (int i = 12; i < 16; i++)
            {
                int peso = (10 + i - 15);
                int resto = (soma % peso);
                int digito;
                if (resto == 0)
                {
                    digito = resto;
                }
                else
                {
                    digito = peso - resto;
                }
                int caracter = Convert.ToInt32(Licenca[i].ToString());
                if (digito != caracter) return false;
                soma += digito;
            }

            return (soma % 10 == 0);
        }

        static public void gravarErro(string Erro)
        {
            EventLog eventError = new EventLog();

            eventError.Source = "wssServer";
            eventError.WriteEntry(Erro);

        }
    }
}
